################################################################################
##
## Purpose: This script creates Figure 2
##
## Author: James Bisbee (james.h.bisbee@vanderbilt.edu)
##
## Input Files:
##  - ./data/prepped/finalData.RData: Prepped data from 9_DATA_final_build.R
##
## Output Files:
##  - ./output/figures/MS_figure_2.pdf
##
##
## See associated log file for compute environment, package versions, 
##  and date of most recent run.
##
################################################################################
rm(list = ls())
gc()
require(tidyverse)
require(ggridges)

set.seed(123)

# Compute details
print(paste0('Compute environment from ',Sys.Date(),' run by Bisbee'))
if(Sys.info()['sysname'] == 'Windows') {
  ram_size = system("wmic MemoryChip get Capacity", intern = TRUE)[-1]
  model_name = system("wmic cpu get name", intern = TRUE)[2] # nocov
  vendor_id = system("wmic cpu get manufacturer", intern = TRUE)[2] # nocov
  
  print(list(ram = stringr::str_squish(ram_size)[1],
             vendor_id = stringr::str_squish(vendor_id),
             model_name = stringr::str_squish(model_name),
             no_of_cores = parallel::detectCores()))
} else if(Sys.info()['sysname'] == 'Linuxs') {
  splitted <- strsplit(system("ps -C rsession -o %cpu,%mem,pid,cmd", intern = TRUE), " ")
  df <- do.call(rbind, lapply(splitted[-1], 
                              function(x) data.frame(
                                cpu = as.numeric(x[2]),
                                mem = as.numeric(x[4]),
                                pid = as.numeric(x[5]),
                                cmd = paste(x[-c(1:5)], collapse = " "))))
  df
} else {
  cat("If not on Linux or Windows, you'll have to figure out your own solution to seeing the compute environment.")
}

sessionInfo()


load('./data/prepped/finalData.RData')

toplot <- utterance_level %>%
  filter(chamber == 'House') %>%
  mutate(fed = ifelse(fed == 0 & GOP == 1 & Male == 1,'GOP - Male',
                      ifelse(fed == 0 & GOP == 1 & Male == 0,'GOP - Female',
                             ifelse(fed == 0 & DEM == 1 & Male == 1,'DEM - Male',
                                    ifelse(fed == 0 & DEM == 1 & Male == 0,'DEM - Female',
                                           ifelse(fed == 0 & GOP == 0 & DEM == 0,'Expert','Fed Chair')))))) %>%
  filter(fed != 'Expert') %>%
  mutate(fed = ifelse(grepl('Male',fed),'Male',
                      ifelse(grepl('Female',fed),'Female',fed))) %>%
  group_by(fed,date,yellenTime) %>%
  summarise(interrupted = mean(interrupted),
            nchars = mean(nchars,na.rm=T),
            n = n(),
            interruptor = mean(interruptor)) %>%
  gather(type,proportion,-fed,-date,-yellenTime,-nchars,-n) %>%
  bind_rows(utterance_level %>%
              filter(chamber == 'House') %>%
              mutate(fed = ifelse(fed == 0 & GOP == 1 & Male == 1,'GOP - Male',
                                  ifelse(fed == 0 & GOP == 1 & Male == 0,'GOP - Female',
                                         ifelse(fed == 0 & DEM == 1 & Male == 1,'DEM - Male',
                                                ifelse(fed == 0 & DEM == 1 & Male == 0,'DEM - Female',
                                                       ifelse(fed == 0 & GOP == 0 & DEM == 0,'Expert','Fed Chair')))))) %>%
              filter(fed != 'Expert') %>%
              mutate(fed = ifelse(grepl('GOP',fed),'GOP',
                                  ifelse(grepl('DEM',fed),'DEM',fed))) %>%
              group_by(fed,date,yellenTime) %>%
              summarise(interrupted = mean(interrupted),
                        nchars = mean(nchars,na.rm=T),
                        n = n(),
                        interruptor = mean(interruptor)) %>%
              gather(type,proportion,-fed,-date,-yellenTime,-nchars,-n))
  
toplot <- utterance_level %>%
  arrange(docID,ind) %>%
  mutate(fed = ifelse(fed == 0,'Legislators','Fed Chair')) %>%
  mutate(interruptor2 = ifelse(interruptor == 1 & lag(interrupted,2) == 0,1,0)) %>%
  group_by(fed,date,yellenTime,chamber) %>%
  summarise(interrupted = mean(interrupted),
            nchars = mean(nchars,na.rm=T),
            n = n(),
            interruptor = mean(interruptor2)) %>%
  gather(type,proportion,-fed,-date,-yellenTime,-nchars,-n,-chamber) %>%
  mutate(grp = paste0(ifelse(date < as.Date('2014-01-01'),'preYellen',
                             ifelse(date < as.Date('2018-01-01'),'Yellen','postYellen')),fed)) %>%
  mutate(type = ifelse(type == 'interrupted','Speaker is being interrupted',
                       'Speaker is interrupting someone else')) 

pdf('./output/figures/MS_figure_2.pdf',width = 7,height = 5)
toplot %>%
  ggplot(aes(x = date,y = proportion,weight = n,size = n,
             label = grp,
             shape = factor(fed),
             color = factor(fed),
             linetype = factor(fed),
             group = factor(grp))) + 
  geom_point(alpha = .5) + 
  scale_y_continuous(labels = scales::percent,limits = c(0,.45)) + 
  geom_vline(xintercept = as.Date(c('2014-01-01','2018-01-01'))) + 
  annotate(geom = 'rect',xmin = as.Date('2014-01-01'),xmax = as.Date('2018-01-01'),
           ymin = -Inf,ymax = Inf,
           alpha = .1,fill = 'grey50') +
  geom_smooth(show.legend = F,se = F,method = 'lm',formula = 'y ~ poly(x,1)') +
  facet_grid(chamber~type) + 
  scale_color_manual(values = c('grey10','grey50')) + 
  scale_shape_manual(values = c(19,15)) + 
  scale_linetype_manual(values = c('solid','dashed'))  + 
  geom_text(data = toplot %>%
              filter(chamber == 'House',
                     grp == 'YellenFed Chair',
                     date == as.Date('2016-02-10')) %>%
              mutate(grp = "Yellen",
                     date = as.Date('2016-01-01'),
                     proportion = Inf),vjust = 1,show.legend = FALSE) + 
  annotate(geom = 'rect',xmin = as.Date('2014-01-01'),xmax = as.Date('2018-01-01'),
           ymin = -Inf,ymax = Inf,alpha = .2) +
  theme_bw() + 
  theme(legend.position = 'bottom') + 
    labs(x = 'Date',y = 'Proportion of utterances',
         color = 'Speaker',shape = 'Speaker',size = '# of utterances',
         linetype = 'Speaker') + 
  guides(shape = guide_legend(override.aes = list(size = 5)))
dev.off()

#EOF